<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <h1 id="logo">
          <a href="/" title="佛科在线学院">
            <img src="~/assets/img/logo.png" width="100%" alt="佛科在线学院" />
          </a>
        </h1>
        <div class="h-r-nsl">
          <ul class="nav">
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>老师</a>
            </router-link>
          </ul>
          <!-- / nav -->
          <ul class="h-r-login">
            <li v-if="!loginInfo.id" id="no-login">
              <a href="/login" title="登录">
                <em class="icon18 login-icon">&nbsp;</em>
                <span class="vam ml5">登录</span>
              </a>
              |
              <a href="/register" title="注册">
                <span class="vam ml5">注册</span>
              </a>
            </li>
            <li v-if="loginInfo.id" id="is-login-one" class="mr10">
              <a id="headerMsgCountId" href="#" title="消息">
                <em class="icon18 news-icon">&nbsp;</em>
              </a>
              <q class="red-point" style="display: none">&nbsp;</q>
            </li>
            <li v-if="loginInfo.id" class="h-r-user">
              <el-dropdown>
                <a class="el-dropdown-link">
                  <img
                    :src="loginInfo.avatar"
                    width="32"
                    height="32"
                    class="vam"
                    alt="头像"
                  />
                  <span id="userName" class="vam disIb">{{
                    loginInfo.nickname
                  }}</span>
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </a>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item
                      ><a href="/ucenter">个人中心</a></el-dropdown-item
                    >
                    <el-dropdown-item
                      ><a href="/collect">课程收藏</a></el-dropdown-item
                    >
                    <el-dropdown-item
                      ><a href="javascript:void(0);" @click="logout()"
                        >退出</a
                      ></el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </li>
            <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search" v-if="showIndexSearch">
            <label class="h-r-s-box">
              <input
                type="text"
                placeholder="输入你想学的课程"
                v-model="courseName"
              />
              <button class="s-btn" @click="searchCourse()">
                <em class="icon18">&nbsp;</em>
              </button>
            </label>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->
    <nuxt />
    <!-- 公共底引入 -->
    <footer id="footer">
      <section class="container">
        <div class>
          <h4 class="hLh30">
            <span class="fsize18 f-fM c-999">友情链接</span>
          </h4>
          <ul class="of flink-list">
            <li>
              <a
                href="https://www.fosu.edu.cn/"
                title="佛山科学技术学院"
                target="_blank"
                >佛山科学技术学院</a
              >
            </li>
            <li>
              <a
                href="https://www.icourse163.org"
                title="中国大学MOOC"
                target="_blank"
                >中国大学MOOC</a
              >
            </li>
            <li>
              <a
                href="https://www.ketangpai.com/#/homePage"
                title="课堂派"
                target="_blank"
                >课堂派</a
              >
            </li>
            <li>
              <a href="https://www.chaoxing.com/" title="超星" target="_blank"
                >超星</a
              >
            </li>
          </ul>
          <div class="clear"></div>
        </div>
        <div class="b-foot">
          <section class="fl col-7">
            <section class="mr20">
              <section class="b-f-link">
                <a
                  href="https://gitee.com/vancomycin_bo"
                  title="关于我们"
                  target="_blank"
                  >关于我们</a
                >|
                <a
                  href="https://gitee.com/vancomycin_bo"
                  title="联系我们"
                  target="_blank"
                  >联系我们</a
                >|
                <a
                  href="https://gitee.com/vancomycin_bo"
                  title="帮助中心"
                  target="_blank"
                  >帮助中心</a
                >|
                <span>Email：caibo0520@qq.com</span>
              </section>
            </section>
          </section>
          <aside class="fl col-3 tac mt15"></aside>
          <div class="clear"></div>
        </div>
      </section>
    </footer>
    <!-- /公共底引入 -->
  </div>
</template>
<script>
import '~/assets/css/reset.css'
import '~/assets/css/theme.css'
import '~/assets/css/global.css'
import '~/assets/css/web.css'
import '~/assets/css/base.css'
import '~/assets/css/activity_tab.css'
import '~/assets/css/bottom_rec.css'
import '~/assets/css/nice_select.css'
import '~/assets/css/order.css'
import '~/assets/css/swiper-3.3.1.min.css'
import '~/assets/css/pages-weixinpay.css'

import cookie from 'js-cookie'
import userApi from '@/api/login'

export default {
  data() {
    return {
      token: '',
      courseName: '',
      showIndexSearch: false,
      loginInfo: {
        id: '',
        age: '',
        avatar: '',
        mail: '',
        nickname: '',
        sex: '',
      },
    }
  },
  watch: {
    $route(to, from) {
      /// 路由到首页才显示搜索框
      if (to.path == '/') {
        this.showIndexSearch = true
      } else {
        this.showIndexSearch = false
      }
    },
  },
  created() {
    if(this.$route.path == '/') {
      // 加载完网页后执行一次，防止watch不触发
      this.showIndexSearch = true
    }
    this.token = this.$route.query.token
    if (this.token) {
      this.wxLogin()
    }
    this.showLoginInfo()
  },
  methods: {
    wxLogin() {
      if (this.token == '') return
      //把token存在cookie中、也可以放在localStorage中
      cookie.set('guli_token', this.token, { domain: 'localhost' })
      cookie.set('guli_ucenter', '', { domain: 'localhost' })
      //登录成功根据token获取用户信息
      userApi.getLoginInfo().then((response) => {
        this.loginInfo = response.data.data.loginInfo
        //将用户信息记录cookie
        cookie.set('guli_ucenter', this.loginInfo, {
          domain: 'localhost',
        })
      })
    },
    showLoginInfo() {
      var jsonStr = cookie.get('guli_ucenter')
      if (jsonStr) {
        this.loginInfo = JSON.parse(jsonStr)
      }
    },
    logout() {
      // 清空cookie中的用户信息和token
      cookie.set('guli_ucenter', '', { domain: 'localhost' })
      cookie.set('guli_token', '', { domain: 'localhost' })
      //跳转页面
      window.location.href = '/'
    },
    searchCourse() {
      this.$router.push({
        path: '/course',
        query: { courseName: this.courseName },
      })
    },
  },
}
</script>
<style>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>